package com.android.examplelist;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MultiChoiceModeActivity extends ListActivity {

	//public static final String[] options = { "多选删除效果1", "多选删除效果2", "多选删除效果3", "多选删除效果4", "多选删除效果5", "多选删除效果6", "多选删除效果7" };
	public ListView mListView;
	public ListAdapter mListAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multi_choice_mode);

		List<CharSequence> planets = new ArrayList<CharSequence>();
		
		for(int i = 0; i < 10; i++) {
			planets.add("多选删除效果" + i);
		}
		
		setListAdapter(new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, planets));
		mListView = getListView();
		mListAdapter = getListAdapter();
		mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);   
		//mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);   
		
		mListView.setMultiChoiceModeListener(new  AbsListView.MultiChoiceModeListener(){

			@SuppressWarnings("unchecked")
			@Override
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				SparseBooleanArray checkedItemPositions = mListView.getCheckedItemPositions();
				int itemCount = mListView.getCount();

				switch (item.getItemId()) {
				case R.id.menu_delete:
		            
					for(int i = itemCount-1; i >= 0; i--){
		                if(checkedItemPositions.get(i)){
		                	((ArrayAdapter<String>) mListAdapter).remove((String) mListAdapter.getItem(i));
		                }
		            }
					
		            checkedItemPositions.clear();
		            ((BaseAdapter) mListAdapter).notifyDataSetChanged();
		            
		            
		            mode.finish();
					break;
				default:
					return false;
				}
				
				return false;
			}

			@Override
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				MenuInflater inflater = mode.getMenuInflater();
				inflater.inflate(R.menu.cab_team_score_history_menu, menu);
				
				mode.setTitle("Select Items");  
	            mode.setSubtitle("1 item selected"); 
				
				return true;
			}

			@Override
			public void onDestroyActionMode(ActionMode mode) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void onItemCheckedStateChanged(ActionMode mode,
					int position, long id, boolean checked) {
				int count = mListView.getCheckedItemCount();
				
				System.out.println("getCheckedItemCount()=" + count);
				//((View) mListView.getV.setBackgroundColor(Color.parseColor("#F5F5DC"));
			}
			
		});
	}
	

}
